package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Pintuan;
import com.qianniu.napi.admin.entity.penum.OrderType;
import com.qianniu.napi.admin.service.IPintuanService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 拼团
 * </p>
 *
 * @author chenxuebin
 * @since 2018-07-06
 */
@RestController
public class PintuanController {

    private static final Logger logger = LoggerFactory.getLogger(PintuanController.class);

    @Autowired
    private IPintuanService pintuanService;

//    @RequestMapping("/api/pintuan/list")
//    @ResponseBody
//    public JsonDO list(@RequestParam(value="ptype",required = false)String ptype,
//                       @RequestParam(value = "pageIndex", required = false) int pageIndex,
//                       @RequestParam(value = "pageSize", required = false) int pageSize,
//                                HttpServletRequest req) {
//        JsonDO json = new JsonDO();
//        Page page=new Page(pageIndex,pageSize);
//        page = pintuanService.selectPage(page, new EntityWrapper<Pintuan>()
//                .eq("uid", ObjectUtil.getUid())
//                .eq("state", 1));
//        json.setData(page.getRecords());
//        return json;
//    }

    @RequestMapping("/api/pintuan/add")
    @ResponseBody
    public JsonDO pintuanAdd(@Valid Pintuan pintuan,
                             BindingResult bindingResult, HttpServletRequest req) {
        JsonDO json = new JsonDO();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            json.setMsg("There are errors! " + bindingResult);
            json.setCode(5);
            return json;
        }

        pintuan.setState(1);
        pintuan.setStatue(1);
        pintuan.setCheapPrice(pintuan.getPrice().subtract(pintuan.getPinprice()));

        pintuan.setMid(ObjectUtil.getMid());
        pintuan.setMpid(ObjectUtil.getMpid());
        pintuan.setUid(ObjectUtil.getUid());
        pintuan.setCreatetime(new Date());
        pintuan.insert();
        logger.info("======pintuan=" + JSON.toJSONString(pintuan));

        Goods goods = new Goods();
        goods.setId(pintuan.getGid());
        //        goods.setPrice(pintuan.getPrice());
        goods.setType(OrderType.pintuan.getVal());
        goods.updateById();
        return json;
    }

    @RequestMapping("/api/pintuan/modify")
    @ResponseBody
    public Jinfo pintuanUpdate(Pintuan pintuan) {
        Jinfo json = new Jinfo();

        pintuan.setModifytime(new Date());
        pintuan.setModifier(ObjectUtil.getUid());
        pintuan.updateById();
        return json;
    }

    @RequestMapping("/api/pintuan/detail")
    @ResponseBody
    public Jinfo getPintuanDetailById(@RequestParam("gid") Long gid, HttpServletRequest req
    ) {
        Jinfo json = new Jinfo();
        Pintuan pintuan = pintuanService.selectOne(new EntityWrapper<Pintuan>().eq("gid", gid));
        json.setInfo(pintuan);
        return json;
    }

}
